/**
 * date: 2010-08-30
 */
package com.weipu.common.facade.model;

import java.io.Serializable;

import org.json.JSONException;
import org.json.JSONObject;

import com.weipu.common.constants.ServerConstant;

/** 
 * @Description 用户model
 * @version [版本号, 2012-10-29]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 * 
 * @author yw66009
 */
public class UserModel implements Serializable
{
    
    private static final long serialVersionUID = 6424987466800778245L;
    
    /**
     * 用户ID
     */
    private String customerid;
    
    /**
     * 用户名
     */
    private String username;
    
    /**
     * 用户邮箱
     */
    private String email;
    
    /**
     * 姓
     */
    private String firstName;
    
    /**
     * 名
     */
    private String lastName;
    
    /**
     * 性别
     */
    private String gender;
    
    /**
     * 生日
     */
    private String birthday;
    
    /**
     * 国家和地区
     */
    private String country;
    
    /**
     * 用户积分
     */
    private double points;
    
    /*    *//**
                                                                                    * 受教育程度
                                                                                    */
    /*
    private short education;
    
    *//**
      * 婚姻状况
      */
    /*
    private short maritalStatus;
    
    *//**
      * 兴趣爱好
      */
    /*
    private List<Short> hobby;
    
    *//**
      * 收入
      */
    /*
    private short income;*/
    
    /**
     * 图片地址
     */
    private String photoPath;
    
    private String token="";
    
    public String getCustomerid()
    {
        return customerid;
    }
    
    public void setCustomerid(String customerid)
    {
        this.customerid = customerid;
    }
    
    
    public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public String getUsername()
    {
        return username;
    }
    
    public void setUsername(String username)
    {
        this.username = username;
    }
    
    public String getEmail()
    {
        return email;
    }
    
    public void setEmail(String email)
    {
        this.email = email;
    }
    
    public String getFirstName()
    {
        return firstName;
    }
    
    public void setFirstName(String firstName)
    {
        this.firstName = firstName;
    }
    
    public String getLastName()
    {
        return lastName;
    }
    
    public void setLastName(String lastName)
    {
        this.lastName = lastName;
    }
    
    public String getGender()
    {
        return gender;
    }
    
    public void setGender(String gender)
    {
        this.gender = gender;
    }
    
    public String getBirthday()
    {
        return birthday;
    }
    
    public void setBirthday(String birthday)
    {
        this.birthday = birthday;
    }
    
    public String getCountry()
    {
        return country;
    }
    
    public void setCountry(String country)
    {
        this.country = country;
    }
    
    public double getPoints()
    {
        return points;
    }
    
    public void setPoints(double points)
    {
        this.points = points;
    }
    
    public String getPhotoPath()
    {
        return photoPath;
    }
    
    public void setPhotoPath(String photoPath)
    {
        this.photoPath = photoPath;
    }
    
    @Override
    public String toString()
    {
        return tojson().toString();
    }
    
    //    private static final String TAG = "UserModel";
    
    /**
     * 用户实体转 json对象
     * @return [参数说明]
     * @return JSONObject [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public JSONObject tojson()
    {
        JSONObject json = new JSONObject();
        
        try
        {
            //id
            if (null != customerid)
            {
                json.put("CustomerId", customerid);
            }
            
            //用户邮箱 
            if (null != email)
            {
                json.put("Email", email);
            }
            
            //用户名
            if (null != username)
            {
                json.put("UserName", username);
            }
            
            //姓氏
            if (null != firstName)
            {
                json.put("FirstName", firstName);
            }
            
            //名字
            if (null != lastName)
            {
                json.put("LastName", lastName);
            }
            
            //性别
            if (null != gender)
            {
                json.put("Gender", gender);
            }
            
            //生日
            if (null != birthday)
            {
                json.put("Birthday", birthday);
            }
            
            //国家和地区
            if (null != country)
            {
                json.put("Country", country);
            }
            
            //用户积分
            if (0 != points)
            {
                json.put("Points", points);
            }
            
            /*     if (0 != education)
                 {
                     json.put("Education", education);
                 }
                 
                 if (null != hobby)
                 {
                     json.put("Hobby", hobby);
                 }
                 
                 if (0 != income)
                 {
                     json.put("Income", income);
                 }
                 
                 if (0 != maritalStatus)
                 {
                     json.put("MaritalStatus", maritalStatus);
                 }*/
            
            if (null != photoPath)
            {
                json.put("PhotoPath", photoPath);
            }
            
        }
        catch (JSONException e)
        {
            //            Log.w(TAG, "tojson exception: " + e.getMessage());
        }
        
        return json;
    }
    
    /**
     * 方法重载，json 转 用户实体
     * @param json
     * @return [参数说明]
     * @return UserModel [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public static UserModel json2user(JSONObject json,String token)
    {
        if (null == json)
        {
            return null;
        }
        UserModel model = new UserModel();
        
		if (null != token) {
			model.setToken(token);
		}
        
        if (json.optString(ServerConstant.User.CONSTANT_CUSTOMERID) != JSONObject.NULL
            && !json.isNull(ServerConstant.User.CONSTANT_CUSTOMERID))
        {
            model.setCustomerid(json.optString(ServerConstant.User.CONSTANT_CUSTOMERID));
        }
        if (json.optString(ServerConstant.User.CONSTANT_USERNAME) != JSONObject.NULL
            && !json.isNull(ServerConstant.User.CONSTANT_USERNAME))
        {
            
            model.setUsername(json.optString(ServerConstant.User.CONSTANT_USERNAME));
        }
        if (json.optString(ServerConstant.User.CONSTANT_EMAIL) != JSONObject.NULL
            && !json.isNull(ServerConstant.User.CONSTANT_EMAIL))
        {
            
            model.setEmail(json.optString(ServerConstant.User.CONSTANT_EMAIL));
        }
        if (json.optString(ServerConstant.User.CONSTANT_FIRSTNAME) != JSONObject.NULL
            && !json.isNull(ServerConstant.User.CONSTANT_FIRSTNAME))
        {
            model.setFirstName(json.optString(ServerConstant.User.CONSTANT_FIRSTNAME));
        }
        if (json.optString(ServerConstant.User.CONSTANT_LASTNAME) != JSONObject.NULL
            && !json.isNull(ServerConstant.User.CONSTANT_LASTNAME))
        {
            model.setLastName(json.optString(ServerConstant.User.CONSTANT_LASTNAME));
        }
        if (json.optString(ServerConstant.User.CONSTANT_GENDER) != JSONObject.NULL
            && !json.isNull(ServerConstant.User.CONSTANT_GENDER))
        {
            model.setGender(json.optString(ServerConstant.User.CONSTANT_GENDER));
        }
        if (json.optString(ServerConstant.User.CONSTANT_BIRTHDAY) != JSONObject.NULL
            && !json.isNull(ServerConstant.User.CONSTANT_BIRTHDAY))
        {
            model.setBirthday(json.optString(ServerConstant.User.CONSTANT_BIRTHDAY));
        }
        if (json.optString(ServerConstant.User.CONSTANT_COUNTRY) != JSONObject.NULL
            && !json.isNull(ServerConstant.User.CONSTANT_COUNTRY))
        {
            model.setCountry(json.optString(ServerConstant.User.CONSTANT_COUNTRY));
        }
        model.setPoints(json.optDouble(ServerConstant.User.CONSTANT_POINTS));
        if (json.optString(ServerConstant.User.CONSTANT_PHOTOPATH) != JSONObject.NULL
            && !json.isNull(ServerConstant.User.CONSTANT_PHOTOPATH))
        {
            model.setPhotoPath(json.optString(ServerConstant.User.CONSTANT_PHOTOPATH));
        }
        return model;
    }
    
    /** json 转 用户实体
     * @param jsonString 错误的json格式将返回一个null
     * @return [参数说明]
     * @return UserModel [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public static UserModel json2user(String jsonString)
    {
        if (null == jsonString || "".equals(jsonString.trim()))
        {
            return null;
        }
        
        UserModel model = null;
        try
        {
            JSONObject json = new JSONObject(jsonString);
            model = json2user(json,null);
        }
        catch (JSONException e)
        {
            //            Log.w(TAG, "tojson json2user: " + e.getMessage());
        }
        return model;
    }
    
}
